package com.xjlsoft.weather.controller;

import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.xjlsoft.weather.dto.DataAvgDto;
import com.xjlsoft.weather.dto.ResultEntity;

import com.xjlsoft.weather.enums.ResultCodeEnum;
import com.xjlsoft.weather.service.AerosolAvgService;
import com.xjlsoft.weather.service.GroundAvgService;
import com.xjlsoft.weather.service.GroundDataService;
import com.xjlsoft.weather.service.RadiationAvgService;
import com.xjlsoft.weather.util.ResultUtil;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;

@RestController
@RequestMapping("/statistical")
@Api(tags = "统计数据接口")
public class StatisticalController {
	/**
	 * 日志
	 */
	private static final Logger logger = LoggerFactory.getLogger(UserController.class);


	@Autowired
	private GroundAvgService groundAvgService;
	
	@Autowired
	private GroundDataService groundDataService;
	
	@Autowired
	private AerosolAvgService aerosolAvgService;
	
	@Autowired
	private RadiationAvgService radiationAvgService;

	/**
	 * 用户登录操作<br>
	 * 表单方式提交参数
	 */
	@ApiOperation(value = "地面数据  日平均温度 (年)接口", notes = "传入字符串 的年，每次返回 改年的全部数据，例如 2009")
	@ApiImplicitParam(name = "yearStr", value = "地面数据id", dataType = "yearStr", paramType = "query", example = "2009")
	@RequestMapping(value = "/getAvgTemByYear", method = RequestMethod.POST)
	public Object getGroundDataById(@RequestParam(value = "yearStr", required = true) String yearStr) {
		Map<String, Object> map=groundAvgService.getAvgTem(yearStr);
		ResultEntity result = null;
		if (map != null) {
			result = new ResultEntity(ResultCodeEnum.SUCCESS, map, true);
		} else {
			result = new ResultEntity(ResultCodeEnum.FAILURE, null, false);
		}

		Map<String, Object> map1= ResultUtil.resultMap(result);
		return map1;
	};
	
	
	/**
	 * 
	 * 表单方式提交参数
	 */
	@ApiOperation(value = "平均 数据   接口", notes = "根据传入的参数 统计数据，返回：times,datas")
	@RequestMapping(value = "/getAvgByDto", method = RequestMethod.POST)
	public Object getAvgByDto(@RequestBody  DataAvgDto groundAvgDto)  {
		
		Map<String,Object> map=null;
		if(StringUtils.isNotBlank(groundAvgDto.getGround())){
			 map=groundAvgService.getGroundAvgByFiled(groundAvgDto);
		}else if(StringUtils.isNotBlank(groundAvgDto.getRadiation())){
			 map=radiationAvgService.getRadiationAvgByFiled(groundAvgDto);
		}else if(StringUtils.isNotBlank(groundAvgDto.getAerosol())){
			map=aerosolAvgService.getAerosolAvgByFiled(groundAvgDto);
		}
		
		ResultEntity result = null;
		if (map != null ) {
			if( (boolean)map.get("success")){
				result = new ResultEntity(ResultCodeEnum.SUCCESS, map, true);
			}else{
				result = new ResultEntity(ResultCodeEnum.FAILURE,(String)map.get("message"),null, false);
			}
			
		} else {
			result = new ResultEntity(ResultCodeEnum.FAILURE, null, false);
		}

		Map<String, Object> map1= ResultUtil.resultMap(result);
		return map1;
	};
	
	
	/**
	 * 
	 * 表单方式提交参数
	 */
	@ApiOperation(value = "统计(0-320cm) 日平均 （一次展示一年 数据） 地面温度接口", notes = "需要传入  统计属性名称，需要统计的年   返回：返回日平均 时间轴数据，0-320cm数据 ")
	@RequestMapping(value = "/getGstNumberAvgByDto", method = RequestMethod.POST)
	public Object getGstNumberAvgByDto(@RequestBody  DataAvgDto groundAvgDto)  {
		
		Map<String,Object> map=groundAvgService.getGstNumberAvgByFiled(groundAvgDto);
		
		ResultEntity result = null;
		if (map != null) {
			result = new ResultEntity(ResultCodeEnum.SUCCESS, map, true);
		} else {
			result = new ResultEntity(ResultCodeEnum.FAILURE, null, false);
		}

		Map<String, Object> map1= ResultUtil.resultMap(result);
		return map1;
	};
	
	/**
	 * 
	 * 表单方式提交参数
	 */
	@ApiOperation(value = "统计数据 时间间隔接口", notes = "直接调用接口  不用传递参数    返回各种数据的 时间间隔(格式 ：开始有数据时间#最新数据时间)  ")
	@RequestMapping(value = "/getDataTypeTime", method = RequestMethod.POST)
	public Object getDataTime()  {
		
		Map<String,String> map=groundAvgService.getDataTime();
		
		ResultEntity result = null;
		if (map != null) {
			result = new ResultEntity(ResultCodeEnum.SUCCESS, map, true);
		} else {
			result = new ResultEntity(ResultCodeEnum.FAILURE, null, false);
		}

		Map<String, Object> map1= ResultUtil.resultMap(result);
		return map1;
	};
	
	
	/**
	 * 
	 * 表单方式提交参数
	 */
	@ApiOperation(value = "本年 月平均气温 接口", notes = "返回 本年 月平均气温   不用传递参数")
	@RequestMapping(value = "/getMonTemByCurrentYear", method = RequestMethod.POST)
	public Object getMonTemByCurrentYear()  {
		
		Map<String,Object> map=groundAvgService.getMonTemCurrentYear();
		
		ResultEntity result = null;
		if (map != null) {
			result = new ResultEntity(ResultCodeEnum.SUCCESS, map, true);
		} else {
			result = new ResultEntity(ResultCodeEnum.FAILURE, null, false);
		}

		Map<String, Object> map1= ResultUtil.resultMap(result);
		return map1;
	};
	
	/**
	 * 
	 * 表单方式提交参数
	 * @throws Exception 
	 */
	@ApiOperation(value = "查询 地面数据 风速值 (近1(1天的数据)、7平均、30天平均的数据  )  接口", notes = "day1   day7  day30  参数")
	@RequestMapping(value = "/getDaySWindsAnd10WinD", method = RequestMethod.POST)
	@ApiImplicitParam(name = "dataType", value = "查询何种数据   day1   day7  day30", dataType = "String", paramType = "query", example = "2009")
	public Object getCurrentYear2WindsAnd10WinD(@RequestParam(value = "dataType", required = true) String dataType) throws Exception  {
		
		Map<String,Object> map=groundDataService.getCurrentDayWeekMon2WindsAnd10WinD(dataType);
		
		ResultEntity result = null;
		boolean flag=false;
		flag=(boolean)map.get("success");
		
		if (flag) {
			result = new ResultEntity(ResultCodeEnum.SUCCESS, map, true);
		} else {
			String message=(String)map.get("message");
			result = new ResultEntity(ResultCodeEnum.FAILURE,message, null, false);
		}

		Map<String, Object> map1= ResultUtil.resultMap(result);
		return map1;
	};
}
